package com.hh.springboot.controller;

import com.alibaba.fastjson.JSON;
import com.hh.springboot.model.Commodity;
import com.hh.springboot.model.OrderForm;
import com.hh.springboot.model.ShopCart;
import com.hh.springboot.model.User;
import com.hh.springboot.service.OrderService;
import com.hh.springboot.service.ShopCartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.*;

@RestController
public class ShopCartDataController {
    @Autowired
    private ShopCartService shopCartService;
    @Autowired
    private OrderService orderService;
    private ShopCart shopCart;
    private OrderForm orderForm;

    @RequestMapping("/getShopCast")
    public String getShopCast(HttpSession session) {
        User login = (User) session.getAttribute("user");
        System.err.println(login);
        List<ShopCart> shopCarts = shopCartService.getShopCarts(login.getId());
        System.err.println(shopCarts);
        return JSON.toJSONString(shopCarts);
    }

    @RequestMapping("/addShopCart")
    public String addShopCart(Commodity commodity, HttpSession session, int num, String remark){
        shopCart = new ShopCart();
        User user = (User) session.getAttribute("user");
        Map<String, String> map = new HashMap<>();
        if (user != null){
            ShopCart shopCart = shopCartService.getShopCart(commodity.getId(), user.getId());
            if (shopCart != null) {
                map.put("message", "购物车内已有此商品");
            } else {
                this.shopCart.setCreated(new Date());
                this.shopCart.setUId(user.getId());
                this.shopCart.setCommodityId(commodity.getId());
                this.shopCart.setNum(num);
                this.shopCart.setPrice(commodity.getPrice());
                this.shopCart.setSumPrice(num * commodity.getPrice());
                this.shopCart.setRemark(remark);
                int count = shopCartService.insertShopCart(this.shopCart);
                if (count > 0){
                    map.put("message", "添加购物车成功");
                } else {
                    map.put("message", "添加购物车失败");
                }
            }
        } else {
            map.put("message", "请先登录");
        }
        return JSON.toJSONString(map);
    }
    /**
     * 加
     */
    @RequestMapping("/addNum")
    public String addNum(ShopCart shopCart){
        shopCart.setNum(shopCart.getNum() + 1);
        return updateShopCart(shopCart);
    }
    /**
     * 减少
     */
    @RequestMapping("/minus")
    public String minus(ShopCart shopCart){
        shopCart.setNum(shopCart.getNum() - 1);
        if (shopCart.getNum() == 0){
            shopCart.setNum(1);
        }
        return updateShopCart(shopCart);
    }

    /**
     * 修改方法 进行数量加减
     */
    public String updateShopCart(ShopCart shopCart){
        shopCart.setSumPrice(shopCart.getPrice() * shopCart.getNum());
        int count = shopCartService.updateShopCart(shopCart);
        Map<String, String> map = new HashMap<>();
        if (count > 0){
            map.put("message", "成功");
        } else {
            map.put("message", "失败");
        }
        return JSON.toJSONString(map);
    }

    /**
     * 不要了方法
     */
    @RequestMapping("/delShopCart")
    public String deleteShopCart(int id){
        Map<String, String> map = new HashMap<>();
        int count = shopCartService.deleteShopCart(id);
        if (count > 0){
            map.put("message", "成功");
        } else {
            map.put("message", "失败");
        }
        return JSON.toJSONString(map);
    }

    /**
     * 结账
     */
    @RequestMapping("/billPlease")
    public String billPlease(int[] ids, HttpSession session, String remark){
        User user = (User) session.getAttribute("user");
        orderForm = new OrderForm();
        StringBuilder foodsInfo = new StringBuilder();
        double sumPrice = 0;
        for (int i = 0; i < ids.length; i++) {
            ShopCart shopCart = shopCartService.getShopCart(ids[i], user.getId());
            foodsInfo.append(shopCart.getName()).append(" ").append(shopCart.getPrice()).append("元 * ").append(shopCart.getNum()).append("个<br />");
            sumPrice += shopCart.getPrice() * shopCart.getNum();
        }
        orderForm.setOrders(new String(foodsInfo));
        orderForm.setUId(user.getId());
        orderForm.setSumPrice(sumPrice);
        orderForm.setCreated(new Date());
        //默认未支付
        orderForm.setStateId(1);
        //备注
        orderForm.setRemark(remark);
        System.err.println(orderForm);
        int count = orderService.insertOrder(orderForm);
        Map<String, String> map = new HashMap<>();
        if (count > 0){
            map.put("message", "成功");
        } else {
            map.put("message", "失败");
        }
        return JSON.toJSONString(map);
    }
}
